package parser;

import java.util.ArrayList;
import java.util.List;

public class ControlFlowGraph {
	private List<CFGNode> graphNodes;
	private boolean isVisited;
	private CFGNode entryNode;
	private List<String> methodCalls;
	
	public ControlFlowGraph()
	{
		setGraphNodes(new ArrayList<CFGNode>());
		isVisited = false;
		entryNode = new CFGNode();
		entryNode.setCode("Entry");
		entryNode.setType("entry");
		graphNodes.add(entryNode);
		methodCalls = new ArrayList<String>();
	}
	
	public void addNodes(CFGNode node)
	{
		boolean exist = false;
		if(graphNodes!=null && node.getCode()!="")
		{
			for (CFGNode n : graphNodes) 
			{
				if(n.getCode().equals(node.getCode()))
				{
					exist=true;
					break;
				}
				
			}
		}
		if((graphNodes==null || !exist) && node.getCode()!="") graphNodes.add(node);
	}
	
	public void addMethodCalls(String methodCall)
	{
		boolean exist = false;
		if(methodCalls!=null && methodCall!="")
		{
			for (String call : methodCalls) 
			{
				if(call.equals(methodCall))
				{
					exist=true;
					break;
				}
				
			}
		}
		if((methodCalls==null || !exist) && methodCall!="") methodCalls.add(methodCall);
	}
	
	public CFGNode getNode(CFGNode n)
	{
		for (CFGNode no: graphNodes) {
			if(no.getClassName().equals(n.getClassName()) && no.getMethodName().equals(n.getMethodName()) && no.getMvc().equals(n.getMvc()) )
			{
				return no;
			}
		}
		return null;
	}
	
	public CFGNode getLastNode()
	{
		if(graphNodes!=null && graphNodes.size()>=1)
			return graphNodes.get(graphNodes.size()-1);
		else
			return null;
	}
	
	public CFGNode getNode(String methodName, String className , String type)
	{
		for (CFGNode n: graphNodes) {
			if(n.getClassName().equals(className) && n.getMethodName().equals(methodName) && n.getMvc().equals(type) )
			{
				return n;
			}
		}
		return null;
	}

	public void addCFGNode(CFGNode node)
	{
		boolean exist = false;
		if(graphNodes!=null && node.getCode()!="")
		{
			for (CFGNode n : graphNodes) 
			{
				if(n.getClassName().equals(node.getClassName()) && n.getMethodName().equals(node.getMethodName()) 
						&& n.getMvc().equals(node.getMvc()) && n.getCode().equals(node.getCode()))
				{
					exist=true;
					break;
				}
				
			}
		}
		if((graphNodes==null || !exist)&&node.getCode()!="") graphNodes.add(node);
	}
	
	public CFGNode getEntryNode() {
		return entryNode;
	}

	public void setEntryNode(CFGNode entryNode) {
		this.entryNode = entryNode;
	}

	public List<CFGNode> getGraphNodes() {
		return graphNodes;
	}

	public void setGraphNodes(List<CFGNode> graphNodes) {
		this.graphNodes = graphNodes;
	}

	public boolean isStatus() {
		return isVisited;
	}

	public void setStatus(boolean status) {
		this.isVisited = status;
	}

	public List<String> getMethodCalls() {
		return methodCalls;
	}

	public void setMethodCalls(List<String> methodCalls) {
		this.methodCalls = methodCalls;
	}
}
